﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Itour.Models;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace CustomerAssistant.DAL
{
    public class ClientDB
    {
        private Database db;

        private static ClientDB m_instance = null;

        private readonly static object locker = new object();

        public static ClientDB Instance
        {
            get
            {
                if (m_instance == null)
                {
                    lock (locker)
                    {
                        if (m_instance == null)
                        {
                            m_instance = new ClientDB();
                        }
                    }
                }

                return m_instance;
            }
        }

        public ClientDB()
        {
            db = DatabaseFactory.CreateDatabase();
        }

        public void Login(ref OperatorLoginData result)
        {
            SqlParameter operatorname = new SqlParameter("@operatorname", SqlDbType.VarChar, 50);
            operatorname.Value = result.OperatorName;
            SqlParameter password = new SqlParameter("@password", SqlDbType.VarChar, 50);
            password.Value = result.Password;
            DbCommand dbcommand = db.GetStoredProcCommand("ValidateLogin");
            dbcommand.Parameters.Add(operatorname);
            dbcommand.Parameters.Add(password);
            using (IDataReader dr = db.ExecuteReader(dbcommand))
            {
                if (dr.Read())
                {
                    result.OperatorID = Convert.ToInt32(dr["operatorid"]);
                    if (result.OperatorID > 0)
                    {
                        result.OperatorName = dr["operatorname"].ToString();
                        //result.RoleID = Convert.ToInt16(dr["roleid"]);
                        result.RoleName = dr["rolename"].ToString();
                        result.ClientID = Convert.ToInt32(dr["ClientID"]);
                        result.ClientShortName = dr["ClientShortName"].ToString();
                        result.ParentClientID = Convert.ToInt32(dr["ParentClientID"]);
                        //result.ClientLevel = (ClientLevelEnum)dr["ClientLevel"];
                        //result.Client.EnableAddOutOrder = Convert.ToBoolean(dr["EnableAddOutOrder"]);
                        //result.Client.InterAddMoney = Convert.ToDecimal(dr["InterAddMoney"]);
                        result.DisplayName = dr["DisplayName"].ToString();
                        result.CityCode = dr["citycode"].ToString();
                        //result.Level = Convert.ToByte(dr["level"]);
                        result.IsValid = (bool)dr["IsValid"];
                        result.InvoiceRate = decimal.Parse(dr["InvoiceRate"].ToString());
                        if (null != dr["FDIPlatformID"] && dr["FDIPlatformID"] != DBNull.Value)
                            result.PlatformID = (int)dr["FDIPlatformID"];
                        result.ClientTypeID = Convert.ToInt32(dr["FDIClientType"]);
                    }
                    else
                        result = null;
                }
                else
                {
                    result = null;
                }
            }
        }
    }
}